package com.team.arms.public_data.controller;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.team.arms.common.PageVO;
import com.team.arms.common.annotation.Log;
import com.team.arms.common.ry.core.controller.BaseController;
import com.team.arms.common.ry.core.domain.AjaxResult;
import com.team.arms.common.ry.core.page.TableDataInfo;
import com.team.arms.common.ry.enums.BusinessType;
import com.team.arms.public_data.domain.TroopInfo;
import com.team.arms.public_data.domain.dto.ListInformationDto;
import com.team.arms.public_data.domain.dto.TroopInfoDto;
import com.team.arms.public_data.domain.vo.ListInformationVo;
import com.team.arms.public_data.domain.vo.TroopInfoVo;
import com.team.arms.public_data.service.TroopInfoService;
import com.team.arms.yuding.domain.VoluntaryInformation;
import com.team.arms.yuding.service.VoluntaryInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yjh
 * @since 2022-04-27
 */
@Api( tags = "招兵部队及名额")
@RestController
@RequestMapping("/publicDate/troop")
public class TroopInfoController extends BaseController {

    @Autowired
    private TroopInfoService troopInfoService;

    @Autowired
    private VoluntaryInformationService voluntaryInformationService;

    /**
     * 查询招兵部队及名额
     * @return
     */
    @ApiOperation("查询招兵部队及名额")
    @GetMapping("/select")
    public AjaxResult selectPage(@ApiParam("选中的页数") @RequestParam("pageNum") Integer pageNum ,
                                 @ApiParam("页面显示的数据量") @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<TroopInfoVo> list = troopInfoService.selectList();
        PageInfo<TroopInfoVo> pageInfo = new PageInfo<TroopInfoVo>(list);
        PageVO<TroopInfoVo> troopInfoPage = new PageVO<>();
        BeanUtils.copyProperties(pageInfo, troopInfoPage);
        troopInfoPage.setResult(pageInfo.getList());
        troopInfoPage.setPage(pageNum);
        return AjaxResult.success(troopInfoPage);
    }

    /**
     * 根据id查询招兵部队及名额
     * @param id
     * @return
     */
    @ApiOperation("根据id查询招兵部队及名额")
    @GetMapping("/selectById")
    public AjaxResult selectById( @RequestParam(value = "troop_info_id")String id){
        return AjaxResult.success(troopInfoService.selectById(id));
    }

    /**
     * 新增招兵部队及名额
     * @param troopInfo
     */
    @ApiOperation("新增招兵部队及名额")
    @PostMapping("/install")
    public AjaxResult installInformation(@RequestBody TroopInfo troopInfo){
        return toAjax(troopInfoService.insertAll(troopInfo));
    }

    /**
     * 修改招兵部队及名额
     * @param id
     * @param troopInfo
     * @return
     */
    @ApiOperation("修改招兵部队及名额")
    @Log(title = "修改招兵部队及名额", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateInformation(
            @RequestParam(value = "troop_info_id")String id,
            @RequestBody TroopInfo troopInfo){
        QueryWrapper<TroopInfo> wrapper = new QueryWrapper();
        wrapper.eq("troop_info_id",id);
        TroopInfo byId = troopInfoService.getById(id);
        return toAjax(troopInfoService.update(troopInfo,wrapper));
    }

    /**
     * 删除招兵部队及名额
     * @param id
     * @return
     */
    @ApiOperation("批量删除招兵部队及名额")
    @DeleteMapping("/delete")
    public AjaxResult deleteInformation(@RequestParam("troop_info_id") List<String> id){
        return toAjax(troopInfoService.deleteByListInformationIds(id));
    }

    @ApiOperation("删除招兵部队及名额")
    @DeleteMapping("/deleteById")
    public AjaxResult delete(@RequestParam("troop_info_id") String id){
        return toAjax(troopInfoService.deleteByListInformationId(id));
    }

    /**
     * 导出招兵部队及名额
     */
    @ApiOperation("导出招兵部队及名额")
    @Log(title = "招兵部队及名额", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response)
    {
        //设置下载信息
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //这里URLEncoder.encode可以防止中文乱码
        String fileName = "test1";
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        //查询数据库
        List<TroopInfo> list = troopInfoService.list();
        //调用方法进行写操作
        try {
            EasyExcel.write(response.getOutputStream(), VoluntaryInformation.class).sheet("资料清单信息")
                    .doWrite(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation("根据条件查询招兵部队及名额")
    @PostMapping("/selectListTroopInfo")
    public TableDataInfo selectVolunteer(
            @RequestBody(required = false) TroopInfoDto troopInfoDto){

        startPage();

        List<TroopInfoVo> list = troopInfoService.selectListTroopInfo(troopInfoDto);

        return getDataTable(list);
    }
    /**
     *
     * @param field
     * @param content
     * @return
     */
    @ApiOperation("根据对应字段查询招兵部队及名额")
    @GetMapping("/selectByFile")
    public AjaxResult selectByField(@ApiParam("查询的字段")@RequestParam(value = "field") String field ,
                                    @ApiParam("查询的内容")@RequestParam(value = "content")String content){
        return AjaxResult.success(troopInfoService.selectByFiled(field,content));
    }

    /**
     * 第一志愿学历分布统计
     * @param id
     * @return
     */
    @ApiOperation("第一志愿学历分布统计")
    @Log(title = "第一志愿学历分布统计", businessType = BusinessType.UPDATE)
    @GetMapping("/volunteerOne")
    public AjaxResult volunteerOne(@RequestParam(value = "troop_info_id")String id){

        TroopInfoVo troopInfoVo = troopInfoService.selectById(id);
        String troopName = troopInfoVo.getTroopName();
        return AjaxResult.success(voluntaryInformationService.selectCultureProgramByVolunteerOne(troopName));
    }
}

